<?php

namespace App\Utils;

class AES
{
    public static function encrypt($str, $key, $iv)
    {
        $str = $str . str_repeat("\0", 16 - strlen($str) % 16);

        $encrypt = openssl_encrypt($str, 'AES-128-CBC', $key, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING, $iv);

        return bin2hex($encrypt);
    }

    public static function decrypt($str, $key, $iv)
    {
        $decrypt = openssl_decrypt(hex2bin($str), 'AES-128-CBC', $key, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING, $iv);
        return rtrim($decrypt, "\0");
    }
}
